package online.westonline.com.westonline.ui;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.easy.west.westclient.api.login.bean.User;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.options.RegisterOptionalUserInfo;
import cn.jpush.im.api.BasicCallback;
import online.westonline.com.westonline.MyApplication;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.utils.CacheUtils;
import online.westonline.com.westonline.utils.ToastUtils;

public class WelComeActivity extends BaseActivity {


    @Override
    protected void initview() {

    }

    @Override
    protected void initdata() {
        //查询是否登陆过
        if (CacheUtils.getInstance().getUser()!=null)
        {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    LoginIm(CacheUtils.getInstance().getUser());
                }
            },1000);

        }
        else
        {
            //未登录
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(LoginActivity.class,true);
                }
            },2000);
        }
    }
    /***
     * IM登录状态的检查
     */
    private void LoginIm(final User user)
    {
        JMessageClient.login(user.getData().getAccess_token(), CacheUtils.getInstance().password(),
                new BasicCallback() {
                    @Override
                    public void gotResult(int i, String s) {
                        Log.e("登录信息",i+":"+s);
                        if (i==0)
                        {
                            dissmissDialog();
                            //设置别名
                            //1.当前域
                            //2.用户序列号(相当于key)
                            //3.别名为user的token
                            JPushInterface.setAlias(getApplicationContext(),user.getData().getId(),user.getData().getAccess_token());
                            //0即登录成功
                            startActivity(HomeActivity.class,true);
                        }
                        else
                        {
                            switch (i)
                            {
                                case 871303:
                                    //用户名错误
                                    ToastUtils.showToast("您的用户名错误");
                                    dissmissDialog();
                                    break;
                                case 871304:

                                    ToastUtils.showToast("您的密码错误");
                                    dissmissDialog();
                                    break;
                                case  801004:
                                    //密码错误
                                    ToastUtils.showToast("您的密码错误");
                                    dissmissDialog();
                                    break;
                                case 801003:
                                    //用户不存在
                                    registim(user);
                                    break;
                                case 801006:
                                    dissmissDialog();
                                    //账号已被封禁
                                    showSelectDialog("提示", "此账号已被封禁", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            startActivity(LoginActivity.class, true);
                                        }
                                    }, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            MyApplication.exit();
                                        }
                                    });
                                    break;
                                default:
                                    registim(user);
                                    break;                            }
                        }
                    }
                });
    }

    /***
     * 注册极光im
     */
    private void registim(final User user) {
        RegisterOptionalUserInfo userInfo = new RegisterOptionalUserInfo();
        userInfo.setNickname(user.getData().getName());
        JMessageClient.register(user.getData().getAccess_token(), CacheUtils.getInstance().password(),userInfo, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i==0)
                {
                    //成功,调用登录
                    LoginIm(user);
                }
                else
                {
                    switch (i)
                    {
                        case 871305:
                            ToastUtils.showToast("用户名不能低于4位,请联系管理员重新添加账号");
                            break;
                        default:
                            ToastUtils.showToast("错误:"+s);
                            break;
                    }
                }
            }
        });
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_wel_come;
    }
}
